package com.johnny.mybatisplus;
import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.Scanner;
public class CodeGenerator {
    /**
     * 读取控制台内容
     *
     * @param tip
     * @return
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + ": ");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (null != ipt && ipt.trim().length() > 0) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "!");
    }

    public static void main(String[] args) {
        String projectPath = System.getProperty("user.dir");
        //1.全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        //是否支持AR模式
        globalConfig.setActiveRecord(false)
                .setEnableCache(false)
                .setAuthor("USER")
                .setOutputDir(projectPath + "/src/main/java")
                //重新生是否覆盖原有文件
                .setFileOverride(true)
                //设置生成的service接口的名字的首字母是否为I，如IUserService
                .setServiceName("%sService")
                .setBaseColumnList(true)
                .setOpen(false);

        //2.数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        //设置数据库类型
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setDriverName("com.mysql.cj.jdbc.Driver")
                .setUrl("jdbc:mysql://localhost:3310/firstdb")
                .setUsername("root")
                .setPassword("654321");
        //3.策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        //全局大写命令
        strategyConfig.setCapitalMode(true)
                //指定表名 字段名是否使用下划线
                .setDbColumnUnderline(true)
                //数据库表映射到实体的命名策略
                .setNaming(NamingStrategy.underline_to_camel)
                //指定表的前缀
                //.setTablePrefix("tbl_")
                .setInclude(scanner("表名"))
                .setRestControllerStyle(true);
        //4.包名策略配置
        PackageConfig packageConfig = new PackageConfig();
        //父包
        packageConfig.setParent("com.johnny.mybatisplus")
                //mapper包位置
                .setMapper("mapper")
                //service包位置
                .setService("service")
                //controller包位置
                .setController("controller")
                //entity包位置
                .setEntity("entity");
        //5.整合配置
        AutoGenerator autoGenerator = new AutoGenerator();
        autoGenerator.setGlobalConfig(globalConfig)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(packageConfig);
        //6.执行
        autoGenerator.execute();
    }
}
